package test.lucene.pinyin;

import java.io.BufferedReader;
import java.io.Reader;
import java.io.StringReader;

import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.Tokenizer;
import org.wltea.analyzer.lucene.IKTokenizer;

import com.kangrui.lucene.pinyin.PinyinNGramTokenFilter;
import com.kangrui.lucene.pinyin.PinyinTokenFilter;

public class MyAnalyzer extends Analyzer {

	@Override
	protected TokenStreamComponents createComponents(String fieldName) {
		Reader reader = new BufferedReader(new StringReader(fieldName));  
        Tokenizer tokenizer = new IKTokenizer(reader, true);  
        //转拼音  
        TokenStream tokenStream = new PinyinTokenFilter(tokenizer);  
        //对拼音进行NGram处理  
        tokenStream = new PinyinNGramTokenFilter(tokenStream);  
        return new Analyzer.TokenStreamComponents(tokenizer, tokenStream);  
	}

}
